package com.example.demo.controller;


import com.example.demo.identity.Group;
import com.example.demo.identity.PullGroupInfo;
import com.example.demo.utils.RandomGroupAvatar;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MessageController {

    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private final SimpMessagingTemplate messagingTemplate;
    private final ObjectMapper objectMapper;

    public MessageController(SimpMessagingTemplate messagingTemplate, ObjectMapper objectMapper) {
        this.messagingTemplate = messagingTemplate;
        this.objectMapper = objectMapper;
    }

    @MessageMapping("/chat")
    public void receiveCommonMessage(Map<String, Object> messageMap) {
//        这个后续扩展，处理私聊，群聊信息
        System.out.println("在这里收到了消息");
        System.out.println(messageMap);
    }

    //    处理创建群聊消息          放弃使用rabbitmq处理了，还是使用application吧
    @MessageMapping("/create/group-test")
    @SendTo("/queue/snow-Queue")
    public String test(String messageMap) throws JsonProcessingException {
        return messageMap;
    }


    @MessageMapping("/create-group")
    public Map<String, Object> createGroup(@Payload Map<String, Object> messageMap) throws JsonProcessingException {
//        提取群聊信息对象
        @SuppressWarnings("unchecked")
        Map<String, Object> groupInfoObj = (Map<String, Object>) messageMap.get("groupInfo");
//        提取基本信息
        String groupId = groupInfoObj.get("group_id").toString();
        String groupName = groupInfoObj.get("group_name").toString();
        String groupType = groupInfoObj.get("group_type").toString();
        String groupAvatar = RandomGroupAvatar.getRandomImageLinks();
//        构造要发送的消息
        Map<String, Object> sendInfo = Map.of(
                "groupId", groupId,
                "groupName", groupName,
                "groupAvatar", groupAvatar
        );
//        获取成员列表
        Object groupMembersObj = groupInfoObj.get("group_members");
        if (groupMembersObj instanceof List<?>) {
            List<PullGroupInfo.MembersInfo> members = objectMapper.convertValue(groupMembersObj, new TypeReference<List<PullGroupInfo.MembersInfo>>() {
            });
            for (PullGroupInfo.MembersInfo member : members) {
                String memberId = member.getKey();
                messagingTemplate.convertAndSendToUser(memberId, "/queue/notifications", objectMapper.writeValueAsString(sendInfo));
                logger.info("信息发送了:" + memberId);
            }
        }
        return sendInfo;
    }


    @MessageMapping("/hello")
    @SendTo("/queue/hello-receipt")
    public Map<String, Object> hello(@Payload Map<String, Object> messageMap) {
        return messageMap;
    }

}
